<template>
    <div>
        <table class="table table-bordered">
            <tr>
                <td>名字</td>
                <td>
                    <input type="text" v-model="state.name">
                </td>
            </tr>

            <tr>
                <td>电话</td>
                <td>
                    <input type="text" v-model="state.phone">
                </td>
            </tr>

            <tr>
                <td>E-mail</td>
                <td>
                    <input type="text" v-model="state.email">
                </td>
            </tr>

            <tr>
                <td>身份证号</td>
                <td>
                    <input type="text" v-model="state.card">
                </td>
            </tr>

            <tr>
                <td>性别</td>
                <td>
                    <input type="radio" name="sex" v-model="state.sex" :value="0">男
                    <input type="radio" name="sex" v-model="state.sex" :value="1">女
                </td>
            </tr>

            <tr>
                <td>年龄</td>
                <td>
                    <input type="text" v-model="state.age">
                </td>
            </tr>

            <tr>
                <td>出生日期</td>
                <td>
                    <input type="date" v-model="state.createDate">
                </td>
            </tr>



            <tr>
                <td></td>
                <td>
                    <input type="button" value="提交" @click="add">
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import { reactive, toRefs } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import axios from 'axios';
const router = useRouter();
const route = useRoute();
let data: any = reactive({
    state: {

        "name": "",
        "phone": "",
        "email": "",
        "card": "",
        "sex": 0,
        "age": 18,
        "createDate": "",

    },

});
let { state } = toRefs(data);

const add = () => {
    if (state.value.name == "") {
        alert("姓名不能为空")
        return;
    }
    let a = /^1[3456789]\d{9}$/;

    if (state.value.phone == "") {
        alert("电话不能为空")
        return;
    }
    if (!a.test(state.value.phone)) {
        alert("电话格式不正确")
        return;
    }

    if (state.value.email == "") {
        alert("邮箱不能为空")
        return;
    }
    let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
    if (!reg.test(state.value.email)) {
        alert("邮箱格式不正确")
        return;
    }
    if (state.value.card == "") {
        alert("身份证不能为空")
        return;
    }
    if (state.value.sex == "") {
        alert("性别不能为空")
        return;
    }
    if (state.value.age == "") {
        alert("年龄不能为空")
        return;
    }
    if (state.value.createDate == "") {
        alert("创建时间不能为空")
        return;
    }
    let obj = {
        "name": state.value.name,
        "phone": state.value.phone,
        "email": state.value.email,
        "card": state.value.card,
        "sex": state.value.sex,
        "age": state.value.age,
        "createDate": state.value.createDate,
    }


    axios({
        url: '/api/Member/AddM',
        method: 'post',
        data: obj
    }).then((res) => {
        if (res.data.data > 0) {
            alert("添加成功")
            router.push('/list')
        } else {
            alert("添加失败")
            return;
        }

    })
}
</script>

<style scoped></style>